package Calculator.Add;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/*
*Author:AMark
*Date:10/18/2023
*Time:8:50AM
*/
public class CalculatorAdd implements ActionListener {

    private JFrame CalFrame;
    private JTextField Text1,Text2,Result;
    private JLabel Add;
    private JButton Equals;
    private static int result = 0;

    CalculatorAdd() {
        CalFrame = new JFrame("Calculator");
        CalFrame.setSize(400,50);
        Text1 = new JTextField(5);
        Text2 = new JTextField(5);
        Result = new JTextField(5);
        Add = new JLabel("+");
        Equals = new JButton("=");

        CalFrame.add(Text1);
        CalFrame.add(Add);
        CalFrame.add(Text2);
        CalFrame.add(Equals);
        CalFrame.add(Result);
        Equals.addActionListener(this);

        CalFrame.setLayout(new FlowLayout());
        CalFrame.setVisible(true);
        CalFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    //注意这里一定要使用public!!!来定义actionPerformed方法
    public void actionPerformed(ActionEvent ca) {
        if (ca.getSource() == Equals) {
            result = Integer.parseInt(Text1.getText()) + Integer.parseInt(Text2.getText());
            System.out.println(result);
            Result.setText(String.valueOf(result));
        }
    }
    public static void main(String[] args) {
        new CalculatorAdd();
    }
}
